/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.servlet.admin;

import com.entities.Claim;
import com.entities.ClaimStatus;
import com.sessionbeans.ClaimFacadeLocal;
import com.sessionbeans.ClaimStatusFacadeLocal;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author msc
 */
public class OperationClaim extends HttpServlet {

    @EJB
    private ClaimStatusFacadeLocal claimStatusFacade;
    @EJB
    private ClaimFacadeLocal claimFacade;

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            //String save = request.getParameter("sv");
            //if (save.equals("a")) {
            Enumeration<String> nameList = request.getParameterNames();
            while (nameList.hasMoreElements()) {
                String fullStatusName = nameList.nextElement();
                //String subStatusName = fullStatusName.substring(0, 13);
                
                if (fullStatusName.contains("claim_status_")) {
                    String idItem = fullStatusName.substring(13);
                    String statusItem = request.getParameter(fullStatusName);
                    
                    Claim claim = null;
                    for (Claim c : claimFacade.findAll()) {
                        if (c.getClaimId().toString().equals(idItem)) {
                            claim = c;
                        }
                    }
                    
                    ClaimStatus claimStatus = null;
                    for (ClaimStatus cs : claimStatusFacade.findAll()) {
                        if (cs.getStatusId().toString().equals(statusItem)) {
                            claimStatus = cs;
                        }
                    }

                    claim.setStatusId(claimStatus);
                    claimFacade.edit(claim);
                }
            }
            //request.getRequestDispatcher("../WCPS/admin/claimManagement.jsp").forward(request, response);
            //response.sendRedirect("admin/claimManagement.jsp");
            response.sendRedirect("../WCPS/ListClaim");
            //}


//            String detail = request.getParameter("dt");
//            String delete = request.getParameter("dl");
//            String status = request.getParameter("status");
//            String save = request.getParameter("sv");
//            if (detail != null) {
//                Claim claimDetail = null;
//                for (Claim c : claimFacade.findAll()) {
//                    if (((int) c.getClaimId()) == Integer.parseInt(detail)) {
//                        claimDetail = c;
//                    }
//                }
//                request.getSession().setAttribute("detailClaim", claimDetail);
//
//            } else {
//                if (delete != null) {
//                    Claim claimDelete = null;
//                    for (Claim c : claimFacade.findAll()) {
//                        if (((int) c.getClaimId()) == Integer.parseInt(delete)) {
//                            claimDelete = c;
//                        }
//                    }
//                    claimFacade.remove(claimDelete);
//                }
//
//
//                if (status != null) {
//                    int claimId = Integer.parseInt(request.getParameter("claimId"));
//                    int statusId = Integer.parseInt(status);
//
//                    ClaimStatus claimStatus = null;
//                    for (ClaimStatus cs : claimStatusFacade.findAll()) {
//                        if (((int) cs.getStatusId()) == statusId) {
//                            claimStatus = cs;
//                        }
//                    }
//
//                    Claim claim = null;
//                    for (Claim c : claimFacade.findAll()) {
//                        if (((int) c.getClaimId()) == claimId) {
//                            claim = c;
//                        }
//                    }
//
//                    claim.setStatusId(claimStatus);
//                    claimFacade.edit(claim);
//                }
//
//                if (save != null) {
//                    int number = claimFacade.findAll().size();
//                    for (int i = 0; i < number; i++) {
//                        int statusItem = Integer.parseInt(request.getParameter("statusItem" + i));
//                        int idItem = Integer.parseInt(request.getParameter("claimIdItem" + i));
//
//                        ClaimStatus claimStatus = null;
//                        for (ClaimStatus cs : claimStatusFacade.findAll()) {
//                            if (((int) cs.getStatusId()) == statusItem) {
//                                claimStatus = cs;
//                            }
//                        }
//
//                        Claim claim = null;
//                        for (Claim c : claimFacade.findAll()) {
//                            if (((int) c.getClaimId()) == idItem) {
//                                claim = c;
//                            }
//                        }
//
//                        claim.setStatusId(claimStatus);
//                        claimFacade.edit(claim);
//                    }
//                }
//            }
        } catch(Exception e) {
            e.printStackTrace();
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
